const Koa = require ('koa')
const app = new Koa()
const logger = require('koa-logger')
const session = require('koa-session')


const mid1 = async (ctx, next) => {
    ctx.type = 'text/html; charset=utf-8'
    await next()
    ctx.body = ctx.body + ' mid111'
}
const mid2 = async (ctx, next) => {
    ctx.body = 'hi'
    await next()
    ctx.body = ctx.body + ' mid222'
}
const mid3 = async (ctx, next) => {
    ctx.body = ctx.body  + ' luke'
    await next()
    ctx.body = ctx.body + ' mid333'
}
app.use(logger())
app.keys = ['hi luke'] //对session进行加密
app.use(session(app))


// app.use(mid1)
// app.use(mid2)
// app.use(mid3)



app.use(ctx => {
    if (ctx.path === '/'){
        let n = ctx.session.views || 0
        ctx.session.views = ++n
        ctx.body = n + ' 次'
    } else {
        ctx.body = '404'
    }
})

app.listen(2333)

/*
    context
    
    response
        header
        socket
        status
        message
        body
        redirect
        attachment
        type
        toJSON
    
    
*/ 